package com.gxa.medical.service.impl;

import com.gxa.medical.exception.BusinessException;
import com.gxa.medical.exception.ExceptionCode;
import com.gxa.medical.mapper.DoctorMapper;
import com.gxa.medical.pojo.Doctor;
import com.gxa.medical.pojo.DoctorAccount;
import com.gxa.medical.service.DoctorService;
import com.gxa.medical.util.MD5Util;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(rollbackFor = {Throwable.class})
public class DoctorServiceImpl implements DoctorService {
    @Autowired
  private  DoctorMapper doctorMapper;
    @Override
    public List<Doctor> selectAll() {
        return doctorMapper.selectAll();
    }

    @Override
    public Integer insertDoctor(Doctor doctor) {
        Integer integer = doctorMapper.insertDoctor(doctor);
        return integer;
    }

    @Override
    public Integer updatePwd(DoctorAccount doctorAccount) throws BusinessException {
        doctorAccount.setPwd(MD5Util.encode(doctorAccount.getPwd()));
        Integer integer = doctorMapper.updatePwd(doctorAccount);
        if (integer==0){
            throw new BusinessException(ExceptionCode.UPDATE_USER_FAILED);
        }
        return integer;
    }

    @Override
    public List<DoctorAccount> loginDoctor(DoctorAccount doctorAccount) throws BusinessException {
        doctorAccount.setPwd(MD5Util.encode(doctorAccount.getPwd()));
        List<DoctorAccount> doctorAccounts = doctorMapper.login(doctorAccount);
        if(doctorAccounts.size()>0){
            return doctorAccounts;
        }
       throw new BusinessException(ExceptionCode.LOGIN_FAILED);
    }

    @Override
    public List<Doctor> selectAll(Doctor doctor) {
        List<Doctor> doctors = doctorMapper.selectByName(doctor);

        return doctors;
    }


}
